/**
 * 
 */
package bhbt.utils;

import au.com.bytecode.opencsv.CSVWriter;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * @author qiyh
 *
 */
public class CsvUtils {
    
    /**
    * @Title: exportToCsv
    * @param response
    * @param filename
    * @param headRow
    * @param resultList
    * @throws Exception
    * @author: Qiyh 2014-1-20
    */
    public static void exportToCsv(HttpServletResponse response, String filename, String[] headRow, List<LinkedHashMap> resultList) throws Exception {
        response.reset(); // 清空输出流
        filename = new String(filename.getBytes("gbk"), "ISO-8859-1");
        response.setHeader("Content-disposition", "attachment;  filename=" + filename + ".csv");
        response.setCharacterEncoding("gbk");
        response.setContentType("APPLICATION/OCTET-STREAM"); // 定义输出类型
        CSVWriter writer = new CSVWriter(response.getWriter());
        // 设置标题
        writer.writeNext(headRow);
        // 设置内容
        if (null != resultList) {
            for (int j = 0; j < resultList.size(); j++) {
                LinkedHashMap retMap = resultList.get(j);
                Set<Object> keySet = retMap.keySet();
                int idx = 0;
                String values[] = new String[keySet.size()+1];
                values[idx] = String.valueOf(j+1);//设置序号
                for (Object key : keySet) { // 遍历key
                    values[++idx] = StringUtil.getDbStr(retMap.get(key));
                }
                // 将数据写入到csv文件中
                writer.writeNext(values);
            }
        }
    }
    
    /**
    * @Title: exportToCsv
    * @param response
    * @param filename
    * @param reportList
    * @throws Exception
    * @author: Qiyh 2014-1-20
    */
    public static void exportToCsv(HttpServletResponse response, String filename, List<List> reportList) throws Exception {
        response.reset(); // 清空输出流
        filename = new String(filename.getBytes("gbk"), "ISO-8859-1");
        response.setHeader("Content-disposition", "attachment;  filename=" + filename + ".csv");
        response.setCharacterEncoding("gbk");
        response.setContentType("APPLICATION/OCTET-STREAM"); // 定义输出类型
        CSVWriter writer = new CSVWriter(response.getWriter());
        for (int i = 0; i < reportList.size(); i++) {
            List item = (List) reportList.get(i);
            String values[] = new String[item.size()];
            for (int j = 0; j < item.size(); j++) {
                if (item.get(j) == null) {
                    values[j] = "";
                } else if (item.get(j) instanceof String) {
                    values[j] = " " + (String) item.get(j) + " ";
                } else {
                    values[j] = item.get(j).toString();
                }
            }
            // 将数据写入到csv文件中
            writer.writeNext(values);
        }
    }
}
